package com.NeuglsWorkStudio.UI.Buttons
{
	import com.NeuglsWorkStudio.Colors.RGB;
	import com.NeuglsWorkStudio.UI.Buttons.Elements.Tipper;
	import com.greensock.TweenLite;
	import com.greensock.easing.Quad;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.text.TextFormat;
	import flash.utils.Timer;

	/**
	 * implemented a button class,which can display some tip
	 * @author Neugls
	 * @version 1
	 * @created 5/25/2011
	 * */
	public class TTipButton extends TImageButton
	{
		private var _FTip:String='';
		private var _FTip2:String='';
		private var _FTipper:Tipper;
		private var _FTipBackgroundColor:int;
		private var _FTipColor:int;
		private var _FAutoAdjustColor:Boolean=true;
		private var _FDisplayTime:int=4;
		private var _FTipperYOffset:Number=-6;
		
		public function TTipButton(Caption:String, DisplayCaption:Boolean=true)
		{
			super(Caption, DisplayCaption);
			
			Init();
		}

		public function get TipperYOffset():Number
		{
			return _FTipperYOffset;
		}

		public function set TipperYOffset(value:Number):void
		{
			_FTipperYOffset = value;
		}

		public function get Tip2():String
		{
			return _FTip2;
		}

		public function set Tip2(value:String):void
		{
			_FTip2 = value;
		}

		/**How long will the tip to be dipslayed**/
		public function get DisplayTime():int
		{
			return _FDisplayTime;
		}

		/**
		 * @private
		 */
		public function set DisplayTime(value:int):void
		{
			_FDisplayTime = value;
		}

		/**
		 * Indicate whether auto to adjust tip color according to background color
		 * this may helpful to you
		 * */
		public function get AutoAdjustColor():Boolean
		{
			return _FAutoAdjustColor;
		}

		/**
		 * @private
		 */
		public function set AutoAdjustColor(value:Boolean):void
		{
			_FAutoAdjustColor = value;
			if(value){
				ToAdjustColor();
			}
		}

		/**
		 * The Color of the tip
		 * */
		public function get TipColor():int
		{
			return _FTipColor;
		}

		/**
		 * @private
		 */
		public function set TipColor(value:int):void
		{
			_FTipColor = value;
			var format:TextFormat=_FTipper.GetTipTextFormat();
			format.color=value;
			_FTipper.SetTextFormat(format);
		}

		/**
		 * Background color of the tip*/
		public function get TipBackgroundColor():int
		{
			return _FTipBackgroundColor;
		}

		/**
		 * @private
		 */
		public function set TipBackgroundColor(value:int):void
		{
			_FTipBackgroundColor = value;
			_FTipper.TipBackgroundColor=value;
			if(_FAutoAdjustColor){
				ToAdjustColor();
			}
		}

		/**
		 * Tip test of the button
		 * */
		public function get Tip():String
		{
			return _FTip;
		}

		/**
		 * @private
		 */
		public function set Tip(value:String):void
		{
			_FTip = value;
			_FTipper.HTMLTip=false;
			_FTipper.Tip=value;
		}
		
		public function SetHTMLTip(tip:String):void{
			_FTipper.HTMLTip=true;
			_FTipper.Tip=tip;
		}

		override protected function onMouseOver(event:MouseEvent):void{
			super.onMouseOver(event);
			if(!this._FTip||this._FTip=='') return;
			if(stage!=null){
				_FTipper.Tip=!this.DoubleStateState?this._FTip:_FTip2;
				_FTipper.alpha=0;
				stage.addChild(_FTipper);
				var p:Point=new Point(0,_FTipperYOffset);
				p=this.localToGlobal(p);
				//p=this.globalToLocal(p);
				//p=new Point(stage.mouseX,stage.mouseY);
				_FTipper.x=p.x;
				_FTipper.y=p.y;
				TweenLite.to(_FTipper,0.3,{alpha:1,ease:Quad.easeInOut,onComplete:TipTime});
			}else{
				throw new Error('stage is null');
			}
		}
		
		override protected function onMouseOut(event:MouseEvent):void{
			super.onMouseOut(event);
			RemoveTip();
		}
		
		protected function RemoveTip(t:Boolean=true):void{
			if(stage!=null){
				if(t){
					TweenLite.to(_FTipper,0.3,{alpha:0,ease:Quad.easeInOut,onComplete:function():void{
						if(stage&&stage.contains(_FTipper)){
							stage.removeChild(_FTipper);
						}
					}});
				}else{
					if(stage.contains(_FTipper)){
						stage.removeChild(_FTipper);
					}
				}
				
				
			}
		}
		
		protected function onMouseMove(event:MouseEvent):void{
			
		}
		
		protected function Init():void{
			_FTipper=new Tipper(_FTip);
			_FTipper.filters=[new DropShadowFilter(0,0,0x222222,0.9,30,30)];
			this.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove,false,0,true);
		}
		
		/**
		 * This function used to adjust the color
		 * */
		protected function ToAdjustColor():void{
			var textColor:int=RGB.GetInvertedColor(_FTipBackgroundColor);
			TipColor=textColor;
		}
		
		protected function TipTime():void{
			var timer:Timer=new Timer(_FDisplayTime*1000,1);
			timer.addEventListener(TimerEvent.TIMER,function(e:TimerEvent):void{
				RemoveTip();
			},false,0,true);
			timer.start();
		}
		
		override protected function onMouseClick(event:MouseEvent):void{
			RemoveTip(false);
			super.onMouseClick(event);
		}
	}
}